///PHILOTES Source Code.  (C)2012 PhiloLabs
#pragma once

#include "Prerequisites.h"
#include "core/types.h"

#ifdef new
#  undef new
#endif
#ifdef delete
#  undef delete
#endif

namespace Memory
{

template <HeapType T>
class  _PhiloCommonExport MemoryAllocObject
{
public:
	explicit MemoryAllocObject(){ }

	~MemoryAllocObject(){ }

	/// operator new, with debug line info
	void* operator new(size_t sz, const char* file, int line, const char* func)
	{
		return Memory::Alloc(T, sz);
	}

	void* operator new(size_t sz)
	{
		return Memory::Alloc(T, sz);
	}

	/// placement operator new
	void* operator new(size_t sz, void* ptr)
	{
		(void) sz;
		return ptr;
	}

	/// array operator new, with debug line info
	void* operator new[] ( size_t sz, const char* file, int line, const char* func )
	{
		return Memory::Alloc(T, sz);
	}

	void* operator new[] ( size_t sz )
	{
		return Memory::Alloc(T, sz);
	}

	void operator delete( void* ptr )
	{
		Memory::Free(T, ptr);
	}

	// Corresponding operator for placement delete (second param same as the first)
	void operator delete( void* ptr, void* )
	{
		Memory::Free(T, ptr);
	}

	// only called if there is an exception in corresponding 'new'
	void operator delete( void* ptr, const char* , int , const char*  )
	{
		Memory::Free(T, ptr);
	}

	void operator delete[] ( void* ptr )
	{
		Memory::Free(T, ptr);
	}

	void operator delete[] ( void* ptr, const char* , int , const char*  )
	{
		Memory::Free(T, ptr);
	}
};

}//namespace Memory


//MemoryAllocObject SHORTCUTS
//-------------------------------------------------------------------------
typedef Memory::MemoryAllocObject<Memory::ObjectHeap> MemAlloc_General;
typedef Memory::MemoryAllocObject<Memory::ResourceHeap> MemAlloc_Resource;
typedef Memory::MemoryAllocObject<Memory::ResourceHeap> MemAlloc_Animation;
typedef Memory::MemoryAllocObject<Memory::ResourceHeap> MemAlloc_Geometry;
typedef Memory::MemoryAllocObject<Memory::ScratchHeap> MemAlloc_Scripting;
typedef Memory::MemoryAllocObject<Memory::ObjectHeap> MemAlloc_SceneObj;
typedef Memory::MemoryAllocObject<Memory::ObjectHeap> MemAlloc_SceneCtrl;
typedef Memory::MemoryAllocObject<Memory::ScratchHeap> MemAlloc_RenderSys;